﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data;
using HySoft.Cash.BaseClass;
using PayRoller.Include;

namespace PayRoller.Manage.Reports
{
    public class PaymentReport : ParentPage, IRequiresSessionState
    {
        // Fields
        private string cmdStr = "";
        protected DataList DataList1;
        protected DropDownList drp_PageJumping;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        protected Label lab_pageInfo;
        protected LinkButton lbtn_first;
        protected LinkButton lbtn_last;
        protected LinkButton lbtn_next;
        protected LinkButton lbtn_pre;
        protected PagedDataSource pds;

        // Methods
        private void bindList(ListControl lctl, int pageCount, int currentPageIndex)
        {
            lctl.Items.Clear();
            for (int i = 1; i <= pageCount; i++)
            {
                string text = i.ToString();
                lctl.Items.Add(new ListItem(text, text));
                if (lctl.Items[i - 1].Text == currentPageIndex.ToString())
                {
                    lctl.Items[i - 1].Selected = true;
                }
            }
        }

        private void btnTrueOrFalse(bool first, bool next, bool pre, bool last)
        {
            this.lbtn_first.Enabled = first;
            this.lbtn_next.Enabled = next;
            this.lbtn_pre.Enabled = pre;
            this.lbtn_last.Enabled = last;
        }

        private void drp_PageJumping_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.pageJump(this.drp_PageJumping);
        }

        private void enableBtn(int pageCount, int currentPageIndex)
        {
            if (pageCount <= 1)
            {
                this.btnTrueOrFalse(false, false, false, false);
            }
            else if (currentPageIndex == 0)
            {
                this.btnTrueOrFalse(false, true, false, true);
            }
            else if (pageCount == (currentPageIndex + 1))
            {
                this.btnTrueOrFalse(true, false, true, false);
            }
            else
            {
                this.btnTrueOrFalse(true, true, true, true);
            }
        }

        private DataSet getDataSource()
        {
            using (TorginfoBN obn = new TorginfoBN(this.Page))
            {
                DataSet set = new DataSet();

                DataTable list = obn.GetIDList();
                set.Tables.Add(list);
                return set;
            }
        }

        public void initdata()
        {
            int num = 0;
            if (this.ViewState["CurrentPageIndex"] != null)
            {
                num = int.Parse(this.ViewState["CurrentPageIndex"].ToString());
            }
            else
            {
                this.ViewState["CurrentPageIndex"] = 0;
            }
            DataView defaultView = this.getDataSource().Tables[0].DefaultView;
            int count = defaultView.Count;
            this.pds = new PagedDataSource();
            this.pds.DataSource = defaultView;
            this.pds.AllowPaging = true;
            this.pds.PageSize = 4;
            this.pds.CurrentPageIndex = num;
            this.DataList1.DataSource = this.pds;
            this.DataList1.DataBind();
            string[] strArray = new string[] { "Total", defaultView.Count.ToString(), " /4 Items ", (this.pds.CurrentPageIndex + 1).ToString(), " Page/", this.pds.PageCount.ToString(), " Pages" };
            this.lab_pageInfo.Text = string.Concat(strArray);
            this.ViewState["pageCount"] = this.pds.PageCount;
            if (this.pds != null)
            {
                this.pds = null;
            }
            this.bindList(this.drp_PageJumping, (int)this.ViewState["pageCount"], ((int)this.ViewState["CurrentPageIndex"]) + 1);
            this.enableBtn((int)this.ViewState["pageCount"], (int)this.ViewState["CurrentPageIndex"]);
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
            this.lbtn_first.Command += new CommandEventHandler(this.pageBtn_Command);
            this.lbtn_next.Command += new CommandEventHandler(this.pageBtn_Command);
            this.lbtn_pre.Command += new CommandEventHandler(this.pageBtn_Command);
            this.lbtn_last.Command += new CommandEventHandler(this.pageBtn_Command);
            this.drp_PageJumping.SelectedIndexChanged += new EventHandler(this.drp_PageJumping_SelectedIndexChanged);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.m_ModuleCode = "0009";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        private void OnpageBtn_Command()
        {
            if (this.cmdStr == "")
            {
                this.cmdStr = "first";
                this.paging(this.cmdStr);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.OnpageBtn_Command();
            }
        }

        private void pageBtn_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName.Length == 0)
            {
                this.cmdStr = "first";
            }
            else
            {
                this.cmdStr = e.CommandName;
            }
            this.paging(this.cmdStr);
        }

        private void pageJump(ListControl lctl)
        {
            this.ViewState["CurrentPageIndex"] = int.Parse(lctl.SelectedItem.Value) - 1;
            this.paging("");
        }

        private void paging(string cmdName)
        {
            string str = cmdName;
            if (str != null)
            {
                if (!(str == "first"))
                {
                    if (str == "next")
                    {
                        this.ViewState["CurrentPageIndex"] = ((int)this.ViewState["CurrentPageIndex"]) + 1;
                    }
                    else if (str == "pre")
                    {
                        this.ViewState["CurrentPageIndex"] = ((int)this.ViewState["CurrentPageIndex"]) - 1;
                    }
                    else if (str == "last")
                    {
                        this.ViewState["CurrentPageIndex"] = int.Parse(this.ViewState["pageCount"].ToString()) - 1;
                    }
                }
                else
                {
                    this.ViewState["CurrentPageIndex"] = 0;
                }
            }
            this.initdata();
        }


        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                UPaymentDisplay uPaymentDisplay = e.Item.FindControl("UPaymentDisplay") as UPaymentDisplay;
                if (uPaymentDisplay != null && drv != null)
                {
                    uPaymentDisplay.OrgID = Convert.ToInt32(drv["sid"]);
                    uPaymentDisplay.BindInfo();
                }
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

        
    }

}
